<?php
/**
 * Created by PhpStorm.
 * User: Emmett
 * Date: 2016/8/30
 * Time: 17:30
 */

namespace Business\Home;

use Common\Common\GlobalParam;

class DirectorHomeStrategy extends HomeStrategyBase
{

    public function index()
    {
        parent::index();
        $depart_tree = fc_get_department_tree();
//        $this->mvc_controller->assign('depart_tree', $depart_tree);

        $user_id = $this->mvc_controller->current_user['id'];

        $depart = fc_get_depart($user_id);
        $result = fc_get_temp_statistics_by_depart_id($depart['id']);

        foreach ($depart_tree as &$depart) {
            $this->set_count($depart, $result);
        }
        $this->mvc_controller->assign('depart_tree', $depart_tree);
        $org_json = json_encode($depart_tree, JSON_UNESCAPED_UNICODE);
        $this->mvc_controller->assign('org_json', $org_json);
    }

    function set_count(&$depart, $result)
    {
        if ($depart != null) {
            $users = &$depart['sys_user'];

            if (count($users) > 0) {
                foreach ($users as &$user) {
                    $row = searchArray($result, 'user_id', $user['id']);
                    if (!empty($row)) {
                        $user = array_merge($user, $row);
                    }
                    unset($user);
                }
            }
//            p($users);
            if (count($depart['_child']) > 0) {
                foreach ($depart['_child'] as &$depart_child) {
                    $result = $this->set_count($depart_child, $result);
                    unset($depart_child);
                }
            }
            unset($users);
        }
    }

}